﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LocalFile.cs" company="">
//   
// </copyright>
// <summary>
//   The local file.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Core
{
    using System.IO;
    using System.Threading.Tasks;

    /// <summary>
    ///   The local file.
    /// </summary>
    public class LocalFile : IFile
    {
        #region Fields

        /// <summary>
        ///   The _description.
        /// </summary>
        private readonly FileDescription _description;

        /// <summary>
        ///   The _path.
        /// </summary>
        private readonly string _path;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="LocalFile"/> class.
        /// </summary>
        /// <param name="path">
        /// The path. 
        /// </param>
        public LocalFile(string path)
        {
            this._path = path;
            var nameWithoutExtension = Path.GetFileNameWithoutExtension(path);
            var extension = PathUtility.GetExtensionWithoutDot(path);
            var info = new FileInfo(this._path);
            var size = (long)0;
            if (info.Exists)
            {
                size = info.Length;
            }

            this._description = new FileDescription(nameWithoutExtension, extension, size);
        }

        #endregion

        // TODO Refactor
        #region Public Methods and Operators

        /// <summary>
        /// The create temporary file.
        /// </summary>
        /// <param name="extension">
        /// The extension. 
        /// </param>
        /// <returns>
        /// The <see cref="LocalFile"/> . 
        /// </returns>
        public static LocalFile CreateTemporaryFile(string extension)
        {
            var name = Path.GetTempFileName() + "." + extension;
            var path = Path.Combine(Path.GetTempPath(), name);
            return new LocalFile(path);
        }

        /// <summary>
        ///   The get description.
        /// </summary>
        /// <returns> The <see cref="FileDescription" /> . </returns>
        public FileDescription GetDescription()
        {
            return this._description;
        }

        /// <summary>
        ///   The get description async.
        /// </summary>
        /// <returns> The <see cref="Task" /> . </returns>
        public Task<FileDescription> GetDescriptionAsync()
        {
            // TODO Refactor to FromResult
            return Task.Factory.StartNew(() => this._description);
        }

        /// <summary>
        ///   The get path.
        /// </summary>
        /// <returns> The <see cref="string" /> . </returns>
        public string GetPath()
        {
            return this._path;
        }

        /// <summary>
        ///   The get path async.
        /// </summary>
        /// <returns> The <see cref="Task" /> . </returns>
        public Task<string> GetPathAsync()
        {
            return Task.Factory.StartNew(() => this._path);
        }

        /// <summary>
        ///   The get read stream.
        /// </summary>
        /// <returns> The <see cref="Stream" /> . </returns>
        public Stream GetReadStream()
        {
            return File.OpenRead(this._path);
        }

        /// <summary>
        ///   The get write stream.
        /// </summary>
        /// <returns> The <see cref="Stream" /> . </returns>
        public Stream GetWriteStream()
        {
            return new FileStream(this._path, FileMode.OpenOrCreate);
        }

        #endregion
    }
}